import { createRouter, createWebHashHistory } from "vue-router";

import Admin from "~/layouts/admin.vue";
import Index from "~/pages/index.vue";
import Login from "~/pages/login.vue";
import NotFound from "~/pages/404.vue";
import GoodList from "~/pages/goods/list.vue";

import Image from "~/pages/image/list.vue";
import Coupon from "~/pages/coupon/list.vue";
import Manager from "~/pages/admin/list.vue";
import Role from "~/pages/role/list.vue";
import Access from "~/pages/access/list.vue";
import Category from "~/pages/category/list.vue";
import Integral from "~/pages/integral/list.vue";
import Users from "~/pages/users/list.vue";
import Appointment from "~/pages/appointment/list.vue";
import Order from "~/pages/order/list.vue";
// 默认路由，所有用户共享
const routes = [
  {
    path: "/",
    name: "admin",
    component: Admin,
  },
  {
    path: "/login",
    component: Login,
    meta: {
      title: "登錄頁",
    },
  },
  {
    path: "/:pathMatch(.*)*",
    name: "NotFound",
    component: NotFound,
  },
];

// 动态路由，用于匹配菜单动态添加路由
const asyncRoutes = [
  {
    path: "/",
    name: "/",
    component: Index,
    meta: {
      title: "後台首頁",
    },
  },
  {
    path: "/product",
    name: "/product",
    component: GoodList,
    meta: {
      title: "商品管理",
    },
  },

  {
    path: "/image",
    name: "/image",
    component: Image,
    meta: {
      title: "圖庫管理",
    },
  },
  {
    path: "/coupon",
    name: "/coupon",
    component: Coupon,
    meta: {
      title: "優惠券管理",
    },
  },
  {
    path: "/manager",
    name: "/manager",
    component: Manager,
    meta: {
      title: "管理員",
    },
  },
  {
    path: "/role",
    name: "/role",
    component: Role,
    meta: {
      title: "角色管理",
    },
  },
  {
    path: "/access",
    name: "/access",
    component: Access,
    meta: {
      title: "菜單管理",
    },
  },
  {
    path: "/category",
    name: "/category",
    component: Category,
    meta: {
      title: "分類管理",
    },
  },
  {
    path: "/integral",
    name: "/integral",
    component: Integral,
    meta: {
      title: "會員等級",
    },
  },
  {
    path: "/users",
    name: "/users",
    component: Users,
    meta: {
      title: "用戶管理",
    },
  },
  {
    path: "/appointment",
    name: "/appointment",
    component: Appointment,
    meta: {
      title: "線下訂單",
    },
  },
  {
    path: "/order",
    name: "/order",
    component: Order,
    meta: {
      title: "訂單管理",
    },
  },
  {
    path: "/banner",
    name: "/banner",
    component: () => import("~/pages/systems/banner.vue"),
    meta: {
      title: "banner管理",
    },
  },
  {
    path: "/service",
    name: "/service",
    component: () => import("~/pages/systems/service.vue"),
    meta: {
      title: "客服設置",
    },
  },
  {
    path: "/about",
    name: "/about",
    component: () => import("~/pages/systems/about.vue"),
    meta: {
      title: "關於我們",
    },
  },
  {
    path: "/query",
    name: "/query",
    component: () => import("~/pages/systems/query.vue"),
    meta: {
      title: "疑問解答",
    },
  },
  {
    path: "/spoilage",
    name: "/spoilage",
    component: () => import("~/pages/stockpile/spoilage.vue"),
    meta: {
      title: "損耗記錄",
    },
  },
  {
    path: "/purchase",
    name: "/purchase",
    component: () => import("~/pages/stockpile/purchase.vue"),
    meta: {
      title: "採購記錄",
    },
  },
  {
    path: "/comment",
    name: "/comment",
    component: () => import("~/pages/order/comment.vue"),
    meta: {
      title: "評論管理",
    },
  },
  {
    path: "/analyse",
    name: "/analyse",
    component: () => import("~/pages/analyse/list.vue"),
    meta: {
      title: "數據分析",
    },
  },
  {
    path: "/activity",
    name: "/activity",
    component: () => import("~/pages/activity/list.vue"),
    meta: {
      title: "活動歷程",
    },
  },
  {
    path: "/webmaster",
    name: "/webmaster",
    component: () => import("~/pages/systems/webmaster.vue"),
    meta: {
      title: "系統管理",
    },
  },
  {
    path: "/notice",
    name: "/notice",
    component: () => import("~/pages/notice/list.vue"),
    meta: {
      title: "通知管理",
    },
  },
  {
    path: "/store",
    name: "/store",
    component: () => import("~/pages/store/list.vue"),
    meta: {
      title: "店鋪管理",
    },
  },
];

export const router = createRouter({
  history: createWebHashHistory(),
  routes,
});

// 动态添加路由的方法
export function addRoutes(menus) {
  // 是否有新的路由
  let hasNewRoutes = false;
  const findAndAddRoutesByMenus = (arr) => {
    arr.forEach((e) => {
      let item = asyncRoutes.find((o) => o.path == e.path);
      if (item && !router.hasRoute(item.path)) {
        router.addRoute("admin", item);
        hasNewRoutes = true;
      }
      if (e.child && e.child.length > 0) {
        findAndAddRoutesByMenus(e.child);
      }
    });
  };

  findAndAddRoutesByMenus(menus);

  return hasNewRoutes;
}
